package edu.xjtu.jih.control;

import edu.xjtu.jih.common.Result;
import edu.xjtu.jih.common.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet({
        "/con/get_mumbers",
        "/con/get_items",
        "/con/getAbuser",
        "/con/deleteUser",
        "/con/deleteItem"


})
public class ConAPI extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ConService conService = new ConService();
        req.setCharacterEncoding("UTF-8");
        String path = req.getServletPath();
        if("/con/getAbuser".equals(path)){
            List<user> userList = conService.getUserList();
            System.out.println(userList);
            Utils.outResult(resp,Result.success(userList));
        }
        if("/con/get_items".equals(path)){
            List<item> itemList = conService.getItemList();
            System.out.println(itemList);
            Utils.outResult(resp,Result.success(itemList));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ConService conService = new ConService();
        req.setCharacterEncoding("UTF-8");
        String path = req.getServletPath();
        if("/con/deleteUser".equals(path)){

            user itemd = Utils.getBeanFromRequest(user.class,req);
            conService.deleteUser(itemd);
            Utils.outJson(resp, Result.success(200));
        }
        if("/con/deleteItem".equals(path)){
            item itemd = Utils.getBeanFromRequest(item.class,req);
            conService.deleteItem(itemd);
            Utils.outJson(resp, Result.success(200));
        }
    }
}
